package com.dky.satell.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dky.satell.dto.ResultDto;
import com.dky.satell.entity.RequestMapping;
import com.dky.satell.mapper.RequestMappingMapper;
import com.dky.satell.service.RequestMappingService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fangyuan
 * @since 2021-06-02
 */
@Slf4j
@Service
public class RequestMappingServiceImpl extends ServiceImpl<RequestMappingMapper, RequestMapping> implements RequestMappingService {

    @Resource
    private RequestMappingMapper requestMappingMapper;


//    @Override
//    public Page<RequestMapping> search(RequestMappingBO request) {
//        Page page = new Page(request.getPageNum(), request.getPageSize());
//        List<RequestMapping> ontologyList = requestMappingMapper.search(page,request.getName());
//        Page<RequestMapping> ontologyDTOPage = new Page<>(request.getPageNum(), request.getPageSize());
//        ontologyDTOPage.setTotal(ontologyList.size());
//        ontologyDTOPage.setRecords(ontologyList);
//        return ontologyDTOPage;
//    }

    @Override
    public Page<RequestMapping> search(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        Page<RequestMapping> search = requestMappingMapper.search(name);
        return search;
    }

    @Override
    public ResultDto insert(RequestMapping requestMapping) {
        Integer count = requestMappingMapper.countByEntryAddress(requestMapping.getEntryAddress(), null);
        if(count > 0) {
            return ResultDto.fail("入口地址重读");
        }
        requestMapping.setCreateTime(new Date());
        requestMapping.setUpdateTime(new Date());
        super.save(requestMapping);
        return ResultDto.success("", requestMapping);
    }

    @Override
    public ResultDto update(RequestMapping requestMapping) {
        Integer count = requestMappingMapper.countByEntryAddress(requestMapping.getEntryAddress(), requestMapping.getId());
        if(count > 0) {
            return ResultDto.fail("入口地址重读");
        }
        requestMapping.setUpdateTime(new Date());
        super.updateById(requestMapping);
        return ResultDto.success();
    }

    @Override
    public void delete(Integer id) {
        super.removeById(id);
    }
}
